package ALMySqlCommon.ALMySqlSafeOp;

import ALBasicServer.ALServerAsynTask.ALAsynTaskManager;
import ALBasicServer.ALServerSynTask.ALSynTaskManager;
import ALBasicServer.ALTask._IALAsynRunnableTask;
import ALBasicServer.ALTask._IALSynTask;
import ALServerLog.ALServerLog;

/*****************
 * 定时处理安全操作管理器中所有缓存操作的处理任务
 * 根据创建本任务时的序列号判断操作是否合法，如果不合法在处理过程中会跳出
 * @author mj
 *
 */
public class ALAsyncMySqlSafeOpDealTask implements _IALAsynRunnableTask
{
	//操作的安全处理对象
	@SuppressWarnings("rawtypes")
	private _TALMySqlSafeOpDBObj _m_doDBObj;
	//操作序列号
	private long _m_lDealSerialize;
	//重试次数
	private int _m_iRetryCount;

	@SuppressWarnings("rawtypes")
	public ALAsyncMySqlSafeOpDealTask(_TALMySqlSafeOpDBObj _dbObj, long _dealSerialize)
	{
		_m_doDBObj = _dbObj;
		_m_lDealSerialize = _dealSerialize;
		_m_iRetryCount = 0;
	}

	@Override
	public void run()
	{
		if(null == _m_doDBObj)
			return ;
		
		//增加次数
		_m_iRetryCount++;
		
		//进行处理操作
		if(_m_doDBObj._async_DealQueryOp(_m_lDealSerialize))
		{
			//此时需要继续执行，延迟3秒继续处理
			ALServerLog.Sys("Delay 3 Sec Retry deal Query Cache SQL!");
			//构造处理任务对象，并延迟3秒处理
			final ALAsyncMySqlSafeOpDealTask delayRunnableTask = this;
			ALSynTaskManager.getInstance().regTask(new _IALSynTask() 
			{
				@Override
				public void run() {
					//执行日志
					ALServerLog.Sys("Start Delay 3 Sec deal Query Cache SQL!");
					//尝试触发重试处理
					_m_doDBObj.onRetryDealQuery(_m_iRetryCount);
					
					ALAsynTaskManager.getInstance().regTask(_m_doDBObj.getThreadIdx(), delayRunnableTask);
				}
			}, 3000);
		}
	}

}
